<template>
  <div class="base-container" id="main">
    <t-form ref="form" :data="queryParams" label-width="80" @submit="()=>{this.pagination.current=1;this.queryParams.pageNum=1;this.getList()}" @reset="()=>{this.pagination.current=1;this.queryParams.pageNum=1;this.getList()}">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">
          <t-form-item label="项目名称" name="projectName">
            <t-input
              v-model="queryParams.projectName"
              clearable
              placeholder="请输入项目名称"
              style="width: 240px"
            />
          </t-form-item>
          <t-form-item label="申报单位" name="declareDeptName">
            <t-input
              v-model="queryParams.declareDeptName"
              clearable
              placeholder="请输入申报单位"
              style="width: 240px"
            />
          </t-form-item>
          <t-form-item label="项目状态" name="projectStatus">
            <t-select placeholder="请选择项目状态" style="width: 240px" clearable v-model="queryParams.projectStatus"
                      :options="dict.type.sf_project_status">
            </t-select>
          </t-form-item>
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit"> 查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
    </t-form>
    <t-table rowKey="index" size="small" :data="data" :columns="columns" hover :pagination="pagination"
             :loading="dataLoading" :sort="sort" :hideSortTips="true" @sort-change="sortChange" multipleSort >
      <template #adviceFunding="{ row }">
        {{ row.adviceFunding }}万元
      </template>
      <template #projectName="{ row }">
        <t-link @click="dialog.header=row.projectName;dialog.objId=row.id;dialog.open_project_detail=true" hover="color" theme="primary">{{ row.projectName }}</t-link>
      </template>
      <template #projectStatus="{ row }">
        <t-tag @click="dialog.header=row.projectName;dialog.objId=row.id;dialog.open_approve_list=true"
               class="cursor-pointer" :theme="handleTheme(row.projectStatus, dict.type.sf_project_status)"
               variant="light">
          {{ row.projectStatus | filterByDict(dict.type.sf_project_status) }}
        </t-tag>
      </template>
      <template #acceptanceStatus="{ row }">
        <t-tag @click="dialog.header=row.projectName;dialog.objId=row.id;dialog.open_approve_list=true"
               class="cursor-pointer" v-if="row.acceptanceStatus"
               :theme="handleTheme(row.acceptanceStatus,dict.type.approve_status)" variant="light">
          {{ row.acceptanceStatus | filterByDict(dict.type.approve_status) }}
        </t-tag>
        <t-tag @click="dialog.header=row.projectName;dialog.objId=row.id;dialog.open_approve_list=true"
               class="cursor-pointer" v-else theme="warning" variant="light">未提交
        </t-tag>
      </template>
      <template #op="{ row }">
        <t-button
          v-hasPermi="['*:*:*']" @click="apply(row)"
          size="small"
          variant="text" class="t-button-link">
          <t-icon name="edit-1" slot="icon"/>
          编辑
        </t-button>
        <t-button size="small" v-if="![null].includes(row.acceptanceStatus)" @click="dialog.header=row.projectName;dialog.objId=row.id;dialog.open_acceptance_detail=true" variant="text" class="t-button-link">
          <t-icon name="info-circle" slot="icon"/>
          详情
        </t-button>
        <t-button @click="createPDF(row)" size="small"
                  variant="text" class="t-button-link">
          <t-icon name="file-pdf" slot="icon"/>
          导出PDF
        </t-button>
      </template>
    </t-table>
    <approve-list :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_approve_list"
                  v-if="dialog.open_approve_list"></approve-list>
    <project-detail-dialog  tabDefaultValue="5" :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_project_detail"
                            v-if="dialog.open_project_detail"></project-detail-dialog>
    <acceptance-detail-dialog :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_acceptance_detail"
                                  v-if="dialog.open_acceptance_detail"></acceptance-detail-dialog>
  </div>
</template>

<script>
import projectDetailDialog from "../project/components/project-detail-dialog.vue";
import approveList from "../approve/components/approve-list.vue";
import acceptanceDetailDialog from "./components/acceptance-detail-dialog.vue";


export default {
  name: "project-list",
  dicts: ["sf_project_status", "approve_status"],
  components: {
    approveList,
    projectDetailDialog,
    acceptanceDetailDialog
  },
  data() {
    return {
      nowRow:{},
      deleteVisible:false,
      queryParams:{
        pageNum:1,
        pageSize:10,
        projectName:"",
        declareDeptName:"",
        projectStatus:""
      },
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30, 50],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination,pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
      dialog: {
        header: "",
        visible: false,
        open_detail: false,
        objId: null,
        projectId: null,
        open_approve_list: false,
        open_project_detail: false,
        open_acceptance_detail:false
      },
      sort:[],
      dataLoading: false,
      columns:[
        {
          colKey: "projectName",
          title: "项目名称",
          align: "center",
          width: 220
        },
        {
          colKey: "adviceFunding",
          title: "投入经费",
          align: "center",
          width: 160,
        },
        {
          colKey: "projectLeaderName",
          title: "项目负责人",
          align: "center",
          width: 160,
        },
        {
          colKey: "manageDeptName",
          title: "主管单位",
          align: "center",
          width: 160,
        },
        {
          colKey: "declareDeptName",
          title: "申报单位",
          align: "center",
          width: 160,
        },
        {
          colKey: "declareDate",
          title: "申报日期",
          align: "center",
          width: 160,
          sorter: true,
        },
        {
          colKey: "startDate",
          title: "项目开始日期",
          align: "center",
          width: 160,
          sorter: true,
        },
        {
          colKey: "endDate",
          title: "项目结束日期",
          align: "center",
          width: 160,
          sorter: true,
        },
        {
          colKey: "projectStatus",
          title: "项目状态",
          align: "center",
          width: 160,
          fixed: 'right'
        },
        {
          colKey: "acceptanceStatus",
          title: "预验收状态",
          align: "center",
          width: 160,
          fixed: 'right'
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 120,
          fixed: 'right'
        },
      ],
      data: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 排序
    sortChange(sort){
      this.sort = sort;
      let orderByColumn = []
      let isAsc = []
      if(sort instanceof Array){
        orderByColumn = sort.map(val=>val.sortBy)
        isAsc = sort.map(val=>val.descending?`desc`:`asc`)
      }else {
        orderByColumn = [sort.sortBy]
        isAsc = [sort.descending?`desc`:`asc`]
      }
      this.queryParams.orderByColumn = orderByColumn.join(",")
      this.queryParams.isAsc = isAsc.join(",")
      this.getList();
    },
    // 获取接口数据
    getList(){
      this.dataLoading = true
      this.queryParams.pageNum = this.pagination.current || 1
      this.$api.kjsf.acceptance.list(this.queryParams).then(response=>{
        this.pagination.total = response.total
        this.data = response.rows
        this.dataLoading = false
      }).catch(e=>{
        this.dataLoading = false
        this.$message.error(e.toString())
      })
    },
    apply(row) {
      this.$router.push({
        path: `/kjsf/other/acceptance/acceptance-apply/${row.id}`,
        query: {title: `${row.projectName}-预验收申请`}
      });
    },
    createPDF(row) {
      const instance = this.$loading({
        fullscreen: true,
        attach: 'body',
        preventScrollThrough: false,
      });
      this.$api.kjsf.implement.projectEmbodimentPdf(row.embodimentId).then(response => {
        window.open(response.data.fullFilePath, '_blank')
        instance.hide();
      }).catch((e) => {
        instance.hide();
        this.$message.error(e.toString());
      });
    },
  }
}
</script>

<style lang="less" scoped>

</style>

